package com.zkthink.ceres.order.service.impl;

import com.zkthink.base.R;
import com.zkthink.ceres.order.dao.GoodsReturnMapper;
import com.zkthink.ceres.order.dao.MoneyReturnMapper;
import com.zkthink.ceres.order.dao.ReturnInterventionMapper;
import com.zkthink.ceres.order.dto.ApplyInterventionDTO;
import com.zkthink.ceres.order.entity.GoodsReturn;
import com.zkthink.ceres.order.entity.MoneyReturn;
import com.zkthink.ceres.order.entity.ReturnIntervention;
import com.zkthink.ceres.order.enums.ReturnGoodsStatusEnum;
import com.zkthink.ceres.order.enums.ReturnInterventionStatusEnum;
import com.zkthink.ceres.order.enums.ReturnMoneyStatusEnum;
import com.zkthink.ceres.order.enums.ReturnTypeEnum;
import com.zkthink.ceres.order.service.ReturnInterventionService;
import com.zkthink.base.service.SuperServiceImpl;

import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.exception.BizException;
import com.zkthink.exception.code.BaseExceptionCode;
import com.zkthink.exception.code.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>
 * 业务实现类
 * 平台介入表
 * </p>
 *
 * @author JustArgo
 * @date 2020-07-05
 */
@Slf4j
@AllArgsConstructor
@Service
public class ReturnInterventionServiceImpl extends SuperServiceImpl<ReturnInterventionMapper, ReturnIntervention> implements ReturnInterventionService {

    private MoneyReturnMapper moneyReturnMapper;

    private GoodsReturnMapper goodsReturnMapper;

    @Override
    public R<Boolean> applyIntervention(ApplyInterventionDTO applyInterventionDTO) {
        if(applyInterventionDTO==null || StringUtils.isBlank(applyInterventionDTO.getReturnCode())){
            return R.fail(ExceptionCode.ILLEGALA_ARGUMENT_EX);
        }
        Integer returnType = applyInterventionDTO.getReturnType();
        if(returnType==null || !Arrays.asList(ReturnTypeEnum.RETURN_MONEY.getCode(),
                ReturnTypeEnum.RETURN_GOODS.getCode()).contains(returnType)){
            return R.fail(ExceptionCode.ILLEGALA_ARGUMENT_EX.getCode(),"售后类型不正确");
        }
        String returnCode = applyInterventionDTO.getReturnCode();
        String orderCode = "";
        String tenantCode = "";
        if(returnType==ReturnTypeEnum.RETURN_MONEY.getCode()){
            MoneyReturn moneyReturn = moneyReturnMapper.selectOne(Wraps.<MoneyReturn>lbQ().eq(MoneyReturn::getReturnCode,returnCode));
            if(moneyReturn==null){
                return R.fail(ExceptionCode.NOT_FOUND.getCode(),"售后单不存在");
            }
            if(moneyReturn.getStatus()!= ReturnMoneyStatusEnum.AUDIT_REJECT.getCode()){
                return R.fail(ExceptionCode.OPERATION_EX.getCode(),"当前售后单不是被审核拒绝状态，不能申请平台介入");
            }
            orderCode = moneyReturn.getOrderCode();
            tenantCode = moneyReturn.getTenantCode();
        }else{
            GoodsReturn goodsReturn = goodsReturnMapper.selectOne(Wraps.<GoodsReturn>lbQ().eq(GoodsReturn::getReturnCode,returnCode));
            if(goodsReturn==null){
                return R.fail(ExceptionCode.NOT_FOUND.getCode(),"售后单不存在");
            }
            if(goodsReturn.getStatus()!= ReturnGoodsStatusEnum.AUDIT_REJECT.getCode()){
                return R.fail(ExceptionCode.OPERATION_EX.getCode(),"当前售后单不是被审核拒绝状态，不能申请平台介入");
            }
            orderCode = goodsReturn.getTenantCode();
            tenantCode = goodsReturn.getTenantCode();
        }
        ReturnIntervention ri = new ReturnIntervention();
        ri.setTenantCode(tenantCode);
        ri.setReturnType(returnType);
        ri.setOrderCode(orderCode);
        ri.setReturnCode(returnCode);
        ri.setProofRemark(applyInterventionDTO.getProofRemark());
        ri.setProofImgs(applyInterventionDTO.getProofImgs());
        ri.setStatus(ReturnInterventionStatusEnum.DEALING.getCode());
        baseMapper.insert(ri);

        return R.success();
    }
}
